using FireStarter.CodeGeneration;
using Firestarter.DB.Schema.BO;
using FireStarterModeller.BO;
using NUnit.Framework;

namespace FireStarter.Test.CodeGeneration
{
    [TestFixture]
    public class TestVWGProjectTemplateLoader : TestBase
    {
        [Test]
        public void TestUiManagerVWG()
        {
            //---------------Set up test pack-------------------
            VWGProjectTemplateLoader templateLoader = new VWGProjectTemplateLoader();
            string namespaceName = GetRandomString();
            string uiReference = GetRandomString();
            string uiVWGProjectName = GetRandomString();
            string applicationName = GetRandomString();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            string contents = templateLoader.GetUiManagerVWG(namespaceName, uiReference, uiVWGProjectName, applicationName);
            //---------------Test Result -----------------------

            AssertUiManagerVWGCorrect(contents, namespaceName, uiReference);
        }

        public static void AssertUiManagerVWGCorrect(string contents, string namespaceName, string uiReference)
        {
            StringAssert.Contains(string.Format("using {0};", uiReference), contents);
            StringAssert.Contains(string.Format("namespace {0}", namespaceName), contents);
        }

        [Test]
        public void TestMainForm()
        {
            //---------------Set up test pack-------------------
            VWGProjectTemplateLoader templateLoader = new VWGProjectTemplateLoader();
            string namespaceName = GetRandomString();
            string uiReference = GetRandomString();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            string contents = templateLoader.GetMainForm(namespaceName);
            //---------------Test Result -----------------------

            AssertMainFormCorrect(contents, namespaceName);
        }

        public static void AssertMainFormCorrect(string contents, string namespaceName)
        {
            StringAssert.Contains(string.Format("namespace {0}", namespaceName), contents);
        }

        [Test]
        public void TestGlobal_asax()
        {
            //---------------Set up test pack-------------------
            VWGProjectTemplateLoader templateLoader = new VWGProjectTemplateLoader();
            string namespaceName = GetRandomString();
            string uiReference = GetRandomString();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            string contents = templateLoader.GetGlobal_asax(namespaceName);
            //---------------Test Result -----------------------

            AssertGlobal_asaxCorrect(contents, namespaceName);
        }

        public static void AssertGlobal_asaxCorrect(string contents, string namespaceName)
        {
            StringAssert.Contains(string.Format(@"<%@ Application Codebehind=""Global.asax.cs"" Inherits=""{0}.Global"" Language=""C#"" %>", namespaceName), contents);
        }

        [Test]
        public void TestGlobal_asax_cs()
        {
            //---------------Set up test pack-------------------
            VWGProjectTemplateLoader templateLoader = new VWGProjectTemplateLoader();
            string namespaceName = GetRandomString();
            string projectName = GetRandomString();
            string projectVersion = GetRandomString();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            string contents = templateLoader.GetGlobal_asax_cs(namespaceName, projectName, projectVersion, GetRandomString(), GetRandomString());
            //---------------Test Result -----------------------

            AssertGlobal_asax_csCorrect(contents, namespaceName, projectName, projectVersion);
        }

        public static void AssertGlobal_asax_csCorrect(string contents, string namespaceName, string projectName, string projectVersion)
        {
            StringAssert.Contains(string.Format("namespace {0}", namespaceName), contents);
            StringAssert.Contains(string.Format(@"new HabaneroAppVWG(GlobalRegistry.ApplicationName, GlobalRegistry.ApplicationVersion);"), contents);
        }

        [Test]
        public void TestWebConfig()
        {
            //---------------Set up test pack-------------------
            VWGProjectTemplateLoader templateLoader = new VWGProjectTemplateLoader();
            string applicationName = GetRandomString();
            DMSolution dmSolution = new DMSolution(applicationName);
            dmSolution.ApplicationName = applicationName;
            dmSolution.GenerationSettings = new GenerationSettings();
            DatabaseConnectionInfo databaseConnection = new DatabaseConnectionInfo();
            string vendor = GetRandomString();
            string server = GetRandomString();
            string port = GetRandomString();
            string database = GetRandomString();
            string username = GetRandomString();
            string password = GetRandomString();
            databaseConnection.Vendor = vendor;
            databaseConnection.Server = server;
            databaseConnection.Port = port;
            databaseConnection.Database = database;
            databaseConnection.UserName = username;
            databaseConnection.Password = password;
            dmSolution.GenerationSettings.DatabaseConnection = databaseConnection;
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------

            string contents = templateLoader.GetWebConfig(dmSolution);
            //---------------Test Result -----------------------
            AssertWebConfigCorrect(
                contents,
                dmSolution.SolutionName,
                databaseConnection.Vendor,
                databaseConnection.Server,
                databaseConnection.Port,
                databaseConnection.Database,
                databaseConnection.UserName,
                databaseConnection.Password);
        }

        [Test]
        public void TestWebConfig_DBDetailsNotSupplied_UsesInMemory()
        {
            //---------------Set up test pack-------------------
            VWGProjectTemplateLoader templateLoader = new VWGProjectTemplateLoader();
            string applicationName = GetRandomString();
            DMSolution dmSolution = new DMSolution(applicationName);
            dmSolution.ApplicationName = applicationName;
            dmSolution.GenerationSettings = new GenerationSettings();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------

            string contents = templateLoader.GetWebConfig(dmSolution);
            //---------------Test Result -----------------------
            AssertWebConfigCorrect(contents, dmSolution.SolutionName, "InMemory", "", "", "", "", "");
        }

        public static void AssertWebConfigCorrect(string contents, string applicationName, string vendor, string server, string port, string database, string username, string password)
        {
            StringAssert.Contains(string.Format(@"<param name=""File"" value=""{0}-log-file.txt""/>", applicationName), contents);
            StringAssert.Contains(string.Format(@"<Application Code=""MainForm"" Type=""{0}.MainForm, {0}""/>", applicationName), contents);

            StringAssert.Contains(string.Format(@"<add key=""vendor"" value=""{0}""/>", vendor), contents);
            StringAssert.Contains(string.Format(@"<add key=""server"" value=""{0}""/>", server), contents);
            StringAssert.Contains(string.Format(@"<add key=""port"" value=""{0}""/>", port), contents);
            StringAssert.Contains(string.Format(@"<add key=""database"" value=""{0}""/>", database), contents);
            StringAssert.Contains(string.Format(@"<add key=""username"" value=""{0}""/>", username), contents);
            StringAssert.Contains(string.Format(@"<add key=""password"" value=""{0}""/>", password), contents);
        }
    }
}